/**
 *  *2010-4-27 上午11:47:54  add 
 */
package com.base.myproject.client.ui;

import com.base.myproject.client.busi.BaseStore;
import com.base.myproject.client.system.BaseResource;
import com.extjs.gxt.ui.client.Style.Scroll;
import com.extjs.gxt.ui.client.core.DomQuery;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.ui.AbstractImagePrototype;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;

/**
 * @author Administrator
 *2010-4-27
 * javazgw@gmail.com
 */
public class BaseGridPanel extends Composite implements Listener<BaseEvent>{

	private static BaseGridPanelUiBinder uiBinder = GWT
			.create(BaseGridPanelUiBinder.class);

	interface BaseGridPanelUiBinder extends UiBinder<Widget, BaseGridPanel> {
		
		
	}

	@UiField
	LayoutContainer lc1 ;
	@UiField
	LayoutContainer lc2 ;

	
	Button addbutton = new Button ("添加");
	
	Button delbutton= new Button("删除");
	
	Button canceldelbutton= new Button("取消删除");
	
	protected BaseGrid bg ;
	public BaseGridPanel() {
		initWidget(uiBinder.createAndBindUi(this));
		
	}
	public BaseGridPanel(BaseStore store, ColumnModel cm)
	{
		this(store,cm,false,false);
	}
	public BaseGridPanel(BaseStore store, ColumnModel cm,boolean contentmenu,
			boolean contentkeynav )
	{
		this();
		//始终不允许basegrid的按钮出现
		 bg = new BaseGrid(store,cm,false,contentkeynav);
		 
		 //设置高度
		//DomQuery.selectNode("div[class=x-grid3-scroller]", bg.getElement()).setAttribute("style", "height:93%");

		//bg.setAutoWidth(true);
		//bg.setHeight("100%");
		lc1.add(bg);
		lc1.setScrollMode(Scroll.NONE);
		addbutton.setIcon(AbstractImagePrototype.create(BaseResource.INSTANCE.addrecord()));
		delbutton.setIcon(AbstractImagePrototype.create(BaseResource.INSTANCE.deleterecord()));
		canceldelbutton.setIcon(AbstractImagePrototype.create(BaseResource.INSTANCE.canceldelete()));
		addbutton.setStyleAttribute("float", "left");
		delbutton.setStyleAttribute("float", "left");
		canceldelbutton.setStyleAttribute("float", "left");
		//lc2.setLayout(new FlowLayout());
		lc2.add(addbutton);
		lc2.add(delbutton);
		lc2.add(canceldelbutton);
		if(contentmenu)
		{
		addbutton.addListener(Events.Select, this);
		delbutton.addListener(Events.Select, this);
		canceldelbutton.addListener(Events.Select, this);
		}
		else
		{
			addbutton.setEnabled(false);
			delbutton.setEnabled(false);
			canceldelbutton.setEnabled(false);
		}
//		x-grid3-scroller
	}
	
	public void setAutoWidth(boolean autoWidth)
	{
		bg.setAutoWidth(autoWidth);
	}
	public void setWidth(int width)
	{
		bg.setWidth(width);
	}
	public void setHeight(int height)
	{
		bg.setHeight(height);
	}
	@Override
	public void handleEvent(BaseEvent be) {
		if(be.getType().equals(Events.Select))
		{
			if(be.getSource().equals(addbutton))
			{
				//GWT.log(""+DomQuery.selectNode("div[class=x-grid3-scroller]", bg.getElement()),null);
				//GWT.log(""+bg.el().dom.,null);
				//DomQuery aa;
				//DomQuery.selectNode("div[class=x-grid3-scroller]", bg.getElement()).setAttribute("style", "height:90%");
				addnewrecord();
				
			}
			else if(be.getSource().equals(delbutton))
			{
				delrecord();
			}
			else if(be.getSource().equals(canceldelbutton))
			{
				bg.canceldelete();
			}
		}
		
	}
	protected void delrecord()
	{
		bg.deleterecord();
	}
	protected void addnewrecord() {
		bg.addnewrecord();
		
	}
	public BaseGrid getBg() {
		return bg;
	}
	public void setBg(BaseGrid bg) {
		this.bg = bg;
	}
	
	/**
	 * 底部添加删除按钮
	 * @return
	 */
	public LayoutContainer getLC2()
	{
		return lc2;
	}
	public Button getAddbutton() {
		return addbutton;
	}
	public Button getDelbutton() {
		return delbutton;
	}
	public Button getCanceldelbutton() {
		return canceldelbutton;
	}
	
}
